package com.bbms.controller.systemManage;

import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bbms.entity.ErrorCode;
import com.bbms.entity.Manager;
import com.bbms.entity.Page;
import com.bbms.entity.Result;
import com.bbms.entity.SuccessCode;
import com.bbms.entity.User;
import com.bbms.service.ManagerService;

/**
 * 用户权限管理（系统管理）控制器
 * @author Administrator
 */
@Controller
@RequestMapping("/bbms/systemManage")
public class AuthorityManageController {

	@Resource(name="ManagerServiceImpl")
	private ManagerService ms;
	
	
	@RequestMapping("/authoritySetting")
	public String systeManage() {
		return "systemManage/authoritySetting.html";
	}
	
	/**
	 * 获取分页数据（查询）
	 * @param currentPage 当前页
	 * @param pageSize 数目
	 * @param s_name 管理员姓名
	 * @param s_account 管理员账号
	 * @param s_phone 管理员电话
	 * @param s_categoryId 管理员类别
	 * @param IDCard 管理员身份证
	 * @param start_time 开始时间
	 * @param end_time 结束时间
	 * @return 分页数据
	 */
	@ResponseBody
	@RequestMapping(value="/list",method=RequestMethod.POST)
	public Page getPage(int page, int rows, String s_name, String s_account, 
			String s_phone,Integer s_categoryId, String IDCard, String start_time, String end_time){
		
		System.out.println("参数："+s_name+" "+s_account+" "+s_phone+" "+
								s_categoryId+" "+IDCard+" "+start_time+" "+end_time);
		
		Page data=new Page();
		
		data=ms.getPage(page, rows, s_name, s_account, s_phone, 
							s_categoryId, IDCard, start_time, end_time);
		return data;
	}
	
	/**
	 * 同时 插入/更新 两个对象
	 * @param user 对象
	 * @param manager 管理员
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/save",method=RequestMethod.POST)
	public Result insert(@ModelAttribute("user")User user,@ModelAttribute("manager")Manager manager){
		Result result=new Result();
		
		System.out.println("用户：   "+ user.toString());
		System.out.println("管理员：   "+ manager.toString());
		
		if (manager.getId()==0) {
			
			try {
				ms.saveTwoObj(user, manager);
				result.setStatus(SuccessCode.insertSuccess.getCode());
				result.setMsg(SuccessCode.insertSuccess.getName());
			} catch (Exception e) {
				e.printStackTrace();
				result.setStatus(ErrorCode.insertFailed.getCode());
				result.setMsg(ErrorCode.insertFailed.getName());
			}
		}else {
			
			try {
				ms.updateTwoObj(user, manager);
				result.setStatus(SuccessCode.updateSuccess.getCode());
				result.setMsg(SuccessCode.updateSuccess.getName());
			} catch (Exception e) {
				e.printStackTrace();
				result.setStatus(ErrorCode.updateFailed.getCode());
				result.setMsg(ErrorCode.updateFailed.getName());
			}
		}
		return result;
	}
	
	
	/**
	 * 同时 删除 两个对象
	 * @param user 用户
	 * @param manager 管理员
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	public Result delete(@ModelAttribute("user")User user,@ModelAttribute("manager")Manager manager){
		Result result=new Result();
				
		try {
			
			ms.delTwoObj(user, manager);
			result.setStatus(SuccessCode.deleteSuccess.getCode());
			result.setMsg(SuccessCode.deleteSuccess.getName());
			
		} catch (Exception e) {
			
			e.printStackTrace();
			result.setStatus(ErrorCode.managerDeleteFaild.getCode());
			result.setMsg(ErrorCode.managerDeleteFaild.getName());
		}
		
		return result;
	}
}
